import {  LOGIN, LOGOUT } from './actionTypes.js'
import { doLogin } from 'api'
import { message } from 'antd'
import { createHashHistory } from 'history'
const history = createHashHistory()
// 登录页
const login = (params)=>{
  console.log(params,111)
   return (dispatch)=>{
      doLogin(params).then(res=>{
        if(res.data.code === 200){
          
          // redux中储存一份
          dispatch({
            type: LOGIN,
            value: res.data.data
          })
          // 本地缓存中也存储一份
          localStorage.setItem("token", res.data.data.token)
          localStorage.setItem("userInfo", JSON.stringify(res.data.data.userInfo))
          localStorage.setItem("role", res.data.data.role)
          message.success(res.data.msg,2, ()=>{
            history.push('/admin')
            history.go(0)
          })
        }
      })
   }
}
// 退出登录
const logout = ()=>{
  return {
      type:LOGOUT
  }
}

export {
  login,
  logout
}